/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.Point;

public class Board extends JPanel{
    Image image;
    Image truck;
    int x, y, it;
    
    int[] individu;
    
    PathFinding PF;

    public Board(int[] individu, int algotype){
        it = 0;
        addKeyListener(new TAdapter());
        setFocusable(true);
        setDoubleBuffered(true);
        //Map.createMap(5, 5, );
        ImageIcon _truck = new ImageIcon(this.getClass().getResource("../gambar/truck.png"));
        truck = _truck.getImage();
        this.individu = individu;
        
        PF = new PathFinding(algotype);        
        
        Factory.simulate(individu, 7, PF);                
    }

    private class TAdapter extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER && it<PF.finalPath.size()) {
                it = it + 1;                
            }
        }
    }
  
    public void isiImage(int x, int y){
        ImageIcon _grass = new ImageIcon(this.getClass().getResource("../gambar/rumput.png"));
        ImageIcon _factory = new ImageIcon(this.getClass().getResource("../gambar/pabrik.png"));
        ImageIcon _road = new ImageIcon(this.getClass().getResource("../gambar/jalan.png"));
        ImageIcon _vendor = new ImageIcon(this.getClass().getResource("../gambar/vendor.png"));
        ImageIcon _empty = new ImageIcon(this.getClass().getResource("../gambar/rumput.png"));
        
        if (mapContent.isJalan(x, y)){
            image = _road.getImage();
        } else if (mapContent.isPabrik(x, y)){
            image = _factory.getImage();
        } else if (mapContent.isRumput(x, y)){
            image = _grass.getImage();
        } else if (mapContent.isVendor(x,y)){
            image = _vendor.getImage();
        } else {
            image = _grass.getImage();
        }
    }
    
    public void paint(Graphics g)
    {  
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        for(int j=0;j<Map.getMap().length;j++){
            for(int i=0;i<Map.getMap().length;i++){
                isiImage(j,i);
                g2d.drawImage(image, i*50, j*50, this);
            }
        }
        if (PF.finalPath.size() != 0){
            if (it<PF.finalPath.size())
                g2d.drawImage(truck, PF.finalPath.get(it).y *50, PF.finalPath.get(it).x*50, this);            
            else
                g2d.drawImage(truck, Map.posPabrik.y*50, Map.posPabrik.x*50, this);
        }

//        if (Simulation.PF.Path.size() != 0){
//            if (it<Simulation.PF.Path.size())
//                g2d.drawImage(truck, Simulation.PF.Path.get(it).y *50, Simulation.PF.Path.get(it).x*50, this);
//            else
//                g2d.drawImage(truck, Simulation.PF.Path.get(Simulation.PF.Path.size()-1).y*50, Simulation.PF.Path.get(Simulation.PF.Path.size()-1).y*50, this);
//        }

        Toolkit.getDefaultToolkit().sync();
        repaint();
        g.dispose();
    }
}
